package org.homeorganiser.flex.manager;

import java.util.ArrayList;
import java.util.List;

import org.homeorganiser.flex.dao.UserDAO;
import org.homeorganiser.flex.dto.User;
import org.homeorganiser.flex.util.Constant;
import org.homeorganiser.flex.util.MailUtil;
import org.homeorganiser.flex.vo.LoggedInUser;
import org.homeorganiser.flex.vo.UserDetail;

public class UserManager implements IUserManager {

	UserDAO usrdao;
	MailUtil mailutil;
	/**
	 * @return the usrdao
	 */
	public UserDAO getUsrdao() {
		return usrdao;
	}
	/**
	 * @param usrdao the usrdao to set
	 */
	public void setUsrdao(UserDAO usrdao) {
		this.usrdao = usrdao;
	}
	
	/**
	 * @return the mailutil
	 */
	public MailUtil getMailutil() {
		return mailutil;
	}
	/**
	 * @param mailutil the mailutil to set
	 */
	public void setMailutil(MailUtil mailutil) {
		this.mailutil = mailutil;
	}
	public LoggedInUser checkUserlogin(String userName, String password) {
		LoggedInUser loggedInUsr = new LoggedInUser();
		String usrMessage        = usrdao.validateUser(userName, password);
		if(usrMessage.equals(Constant.MESSAGE_VALID_LOGIN)){
			loggedInUsr.setValidated(true);
			loggedInUsr.setUserName(userName);
			loggedInUsr.setUsrMessage("Welcome "+userName);
		}else{
			loggedInUsr.setUsrMessage(usrMessage);
		}
		return loggedInUsr;
	}
  
	public Boolean checkUserNameAvailiablity(String userName){
		boolean isavail = usrdao.checkUserName(userName);
		if(isavail){
			return isavail;
		}
		return isavail;
	}
	
	public String registerUser(UserDetail usrDetail){
		User dbusr  = new User();
		User addedUser;
		if(!checkUserNameAvailiablity(usrDetail.getUserName())){
			return "User Name not Availiable";
		}
		dbusr.setUserId(usrDetail.getUserName());
		dbusr.setPassword(usrDetail.getPassword());
		dbusr.setFirstName(usrDetail.getFirstName());
		dbusr.setLastName(usrDetail.getLastName());
		dbusr.setAddress(usrDetail.getAddress());
		dbusr.setCity(usrDetail.getCity());
		dbusr.setState(usrDetail.getState());
		dbusr.setZip( usrDetail.getZip());
		dbusr.setPhone(usrDetail.getPhone());
		dbusr.setEmail(usrDetail.getEmail());
		dbusr.setSecurityQuestion(usrDetail.getSecurityQuestion());
		dbusr.setSecurityAnswer(usrDetail.getSecurityAnswer());
	
		addedUser = usrdao.create(dbusr);
		if(addedUser != null){
			return "User Added Successfully";
		}
		return "User Addition Failure";
	}
	public String forgotPassword(String userName) {
		//mailutil.sendMail(from, to, subject, msg)
		String usrEmail = usrdao.getEmail(userName);
		String usrpassword = usrdao.getPassword(userName);
		String fromEmail = "admin@homereality.com";
		String subject ="Forgot Password :";
		String msg ="Your Password :"+usrpassword;
		mailutil.sendMail(fromEmail, usrEmail, subject, msg);
		return "Please Check your Mail";
	}
	public List<UserDetail> getAllUsers() {
		List<UserDetail> usrList = new ArrayList<UserDetail>();
		List<User> dbusrlist;
		UserDetail usrDetail;
		dbusrlist = usrdao.getUsers(null, null, null, null);
		
		for(User usr : dbusrlist){
			usrDetail = new UserDetail();
			usrDetail.setUsrReg(usr.getId());
			usrDetail.setUserName(usr.getUserId());
			usrDetail.setFirstName(usr.getFirstName());
			usrDetail.setLastName(usr.getLastName());
			usrDetail.setEmail(usr.getEmail());
			
			usrList.add(usrDetail);
		}
		return usrList;
	}
	public List<UserDetail> getUsers(String usrName, String firstName,
			String lastName, String usremail) {
		List<UserDetail> usrList = new ArrayList<UserDetail>();
		List<User> dbusrlist;
		UserDetail usrDetail;
		dbusrlist = usrdao.getUsers(usrName, firstName, lastName, usremail);
		
		for(User usr : dbusrlist){
			usrDetail = new UserDetail();
			usrDetail.setUsrReg(usr.getId());
			usrDetail.setUserName(usr.getUserId());
			usrDetail.setFirstName(usr.getFirstName());
			usrDetail.setLastName(usr.getLastName());
			usrDetail.setEmail(usr.getEmail());
			
			usrList.add(usrDetail);
		}
		return usrList;
	}
	public UserDetail getUserDetail(int userReg) {
		UserDetail usrDetail = null;
		User usr;
		usr = usrdao.getUsersDetail(userReg);
		if(usr !=null){
			usrDetail = new UserDetail();
			usrDetail.setUsrReg(usr.getId());
			usrDetail.setUserName(usr.getUserId());
			usrDetail.setPassword(usr.getPassword());
			usrDetail.setPhone(usr.getPhone());
			usrDetail.setFirstName(usr.getFirstName());
			usrDetail.setLastName(usr.getLastName());
			usrDetail.setEmail(usr.getEmail());
		}
		return usrDetail;
	}
	public String updateUserDetail (UserDetail usrDetail) {
		User usr;
		if(usrDetail != null){
			usr = new User();
			usr.setId(usrDetail.getUsrReg());
			usr.setUserId(usrDetail.getUserName());
			usr.setPassword(usrDetail.getPassword());
			usr.setPhone(usrDetail.getPhone());
			usr.setEmail(usrDetail.getEmail());
			if(usrdao.update(usr)){
				return "User Updated Successfully";
			}
		}
		return "User not Updated Successfully";
	}
	
}
